package;
import fl.controls.Button;

import flash.Lib;
import flash.display.MovieClip;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.events.NetStatusEvent;
import flash.net.NetConnection;
import flash.net.Responder;

class Main extends MovieClip
{
	static var app:String="/tests";
	var nc:NetConnection;
	
	public static function main()
	{
		Lib.current.stage.scaleMode=StageScaleMode.NO_SCALE;
		Lib.current.stage.align=StageAlign.TOP_LEFT;
		Lib.current.addChild(new Main());
	}
	public function new()
	{
		super();
		nc = new NetConnection();
		nc.client=this;
		nc.objectEncoding = 0;
		nc.addEventListener(NetStatusEvent.NET_STATUS, onNetStatus);
		addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
	}
	function onAddedToStage(event:Event)
	{
		removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
		var uri = getUri();
		trace("connecting to rtmp://" + uri);
		nc.connect("rtmp://"+uri, {foo:"bar"});
		//nc.connect("rtmp://"+uri, null);
		//nc.connect("rtmp://"+uri);
		createButtons();
	}
	function createButtons()
	{
		var index=0;
		for(i in 
		[
			"test Void",
			"test Int",
			"test String",
			"test Object",
			"test Array",
			"test Mixed",
			"test Date",
			"test Return",
			"test Missing",
			"test Mismatch",
			"test Hash"
		])
		{
			var btn = new Button();
			addChild(btn);
			btn.label=i;
			btn.x=500;
			btn.y = 100 + ++index*(btn.height+5);
			//var _nc = nc;
			//var onResult = onTResult;
			//var onError = this.onTError;
			btn.addEventListener(MouseEvent.CLICK, 
			function (event:MouseEvent)
			{
				switch(cast(event.currentTarget, Button).label)
				{
					case "test Void":		nc.call("testVoid", null);//nc.call("testVoid", new Responder(onResult, onError), input.text);
					case "test Int":		nc.call("testInt", null, 10);
					case "test String":	nc.call("testString", null, "hello world");
					case "test Object":	nc.call("testObject", null, {name:"foo", age:99});
					case "test Array":	nc.call("testArray", null, ["a", "b", "c"]);
					case "test Date":		nc.call("testDate", null, Date.now());
					case "test Mixed":	nc.call("testMixed", null, {
																			arr:[{name:"foo", age:99}, "b", "c"], 
																			str:"hello world", 
																			nr:10.5, 
																			obj:{name:"foo", age:[99]	}
																			});
					case "test Return":	nc.call("testReturn", new Responder(onResult, onError), {nr1:10, nr2:20});
					case "test Missing":	nc.call("testMissing", new Responder(onResult, onError));
					case "test Mismatch":nc.call("testMismatch", new Responder(onResult, onError), 10);
					case "test Hash":
					var hash = new Hash();
					hash.set("one", 1);
					hash.set("two", 2);
					nc.call("testHash", new Responder(onResult, onError), hash);
				}
			});
		}
	}
	function onResult(obj:Dynamic)
	{
		trace("onResult:"+obj);
		for(i in Reflect.fields(obj))
			trace(i+"->"+Reflect.field(obj, i));
	}
	function onError(obj:Dynamic)
	{
		trace("onError"+obj);
		for(i in Reflect.fields(obj))
			trace(i+"->"+Reflect.field(obj, i));
	}
	
	function onNetStatus(event:NetStatusEvent):Void
	{
		trace("code:"+event.info.code+", description:" + event.info.description);
		if(event.info.code =='NetConnection.Connect.Success')
		{
			trace("start testing");
			trace("========= Watch the server logs/traces for all the upper button tests ================");
		}
	}
	
	//called from server script:
	public function testClientInt(i:Int):Void
	{
		trace("testClientInt:" + (i+10));
	}
	public function testClientString(s:String):Void
	{
		trace("testClientString:" + s + 10);
	}
	public function testClientObject(obj:Dynamic):Void
	{
		trace("testClientObject:" + obj.toString());
		for(i in Reflect.fields(obj))
			trace(i+"->"+Reflect.field(obj, i));
	}
	public function testClientArray(arr:Array<Dynamic>):Void
	{
		trace("testClientArray:" + arr.toString());
		trace(arr[2].a.pop());
		for(i in 0...arr.length)
			trace(i+"->"+arr[i]);
	}
	public function testClientDate(d:Date):Void
	{
		trace("testClientDate:" + d);
		trace(d.getFullYear());
	}
	function getUri():String
	{
		var parameters:Dynamic = flash.Lib.current.loaderInfo.parameters;
		//trace(parameters);
		var u = Reflect.field(parameters, "uri");
		if(u!=null)
			return u;
		var h = Reflect.field(parameters, "host");
		var p = Reflect.field(parameters, "port");
		var a = Reflect.field(parameters, "app");
		return (h==null?"localhost":h) + (p==null?":1935":":"+p) + (a==null?app:a);
	}
}